package com.example.teach.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.teach.entity.Course;
import com.example.teach.entity.Result;
import com.example.teach.mapper.CourseMapper;
import com.example.teach.service.CourseService;
import jakarta.annotation.Resource;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Resource
    private CourseService courseService;


    @PostMapping("/add")
    public Result addCourse(@RequestBody Course course) {
        if(courseService.selectByName(course.getCourseName()) != null){
            return Result.error("课程名已存在");
        }
        courseService.save(course);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result deleteCourseById(@RequestParam("id") Integer id) {
        if(id != null){
            courseService.removeById(id);
            return Result.success();
        }
        return Result.error();
    }

    @PutMapping("/update")
    public Result updateCourseById(@RequestBody Course course) {
        courseService.updateById(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(){
        return Result.success(courseService.list());
    }

    @GetMapping("/page")
    public Result selectLike(@RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                             @RequestParam(value = "courseName",defaultValue = "") String courseName) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.orderByAsc(Course::getId);
        if(StrUtil.isNotBlank(courseName)){
            queryWrapper.like(Course::getCourseName,courseName);
        }
        Page<Course> page = courseService.page(new Page<>(pageNum,pageSize),queryWrapper);
        return Result.success(page);
    }
    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.success(courseService.list());
    }



}
